//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
#include "stdafx.h"
#include "GeneralProperties.hpp"
#include "DisplayProperties.hpp"
#include "CControlPanelPage.hpp"
#include "PrinterParser.hpp"
#include "PropClasses.hpp"

#include <lass.h>
#include <lass_ae.h>

#define NUMPAGES 3

#ifdef UNDER_CE
#include "windev.h" // for EnumDevices
#else
#include "vc_windev.h" // for EnumDevices
#endif


void DoPropSheet()
{
	BOOL bPage			= TRUE;
	int pageCount		= 0;
	const int nPages	= NUMPAGES;
	CTerminalPropSheet* sheet = NULL;
	CPropPage *pages[nPages];

	// load propperty sheet caption
	LPCTSTR pszCaption = LoadStringEx(IDS_PROPSHT_CAPTION);
	
	HINSTANCE hInst = ghInstance;
	HINSTANCE hInstRes = ghInstance;

	for ( int i = 0;i < nPages;i++)
		pages[i] = NULL;

	__try
	{
		// create property sheet
		sheet = new CTerminalPropSheet (NULL, pszCaption);

		// create pages in reverse order due to ATL window mappings
		// being chained
		pages[0] = new CGeneralPropPage (sheet);
		pages[1] = new CDisplayPropPage (sheet);
		pages[2] = new CControlPanelPage(sheet);


	}__except (EXCEPTION_EXECUTE_HANDLER)
	{
		DEBUGMSG (1, (L"Atlprop::DoPropSheet() -- No Owner specified"));
		goto EXIT_PROC;
	}
    
	for (i = 0;i < nPages;i++)
    {
        if(pages[i] == NULL)
        {
            bPage = FALSE;
            break;
        }
        sheet->AddPropPage (pages[i]);
    }

    if( bPage )
    {
	    // activate printers property page on startup
	    sheet->Run();
    }

EXIT_PROC:	
    for (i = 0;(i < nPages);i++)
        if(pages[i])
	        delete pages[i];
	if (sheet)
		delete sheet;

}


HINSTANCE ghInstance;

int WINAPI _tWinMain (HINSTANCE hInst, HINSTANCE hInstPrev, LPTSTR lpcmdLine, int nShow)
{
	ghInstance = hInst;

	CTerminalPropSheet* sheet = NULL;
	CDisplayPropPage *page = NULL;

	if ( wcscmp(lpcmdLine, TEXT("SYNC")) == 0 ) {
		sheet = new CTerminalPropSheet (NULL, TEXT(""));
		page  = new CDisplayPropPage (sheet);
		page->SyncDisplaySettings();
	} else if (wcscmp(lpcmdLine, TEXT("RESET")) == 0 ) {
		sheet = new CTerminalPropSheet (NULL, TEXT(""));
		page  = new CDisplayPropPage (sheet);
		page->ResetDisplay2DefaultSetting();
	} else {
		if( VerifyUser(&AE_CPLMAIN,NULL,NULL,VU_UNTIL_SUCCESS,0) == TRUE)
		{
			DoPropSheet();
		}
	}

    if ( page  ) delete page;
	if ( sheet ) delete sheet;

	return 0;
}
